{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "超参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib as mpl\n",
    "import matplotlib.pyplot as plt\n",
    "from sklearn import datasets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "digits=datasets.load_digits()#使用手写数字数据集\n",
    "X=digits.data\n",
    "y=digits.target"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "x_train,x_test,y_train,y_test=train_test_split(X,y,train_size=0.2,random_state=666)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9673157162726008"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "my_knn=KNeighborsClassifier(n_neighbors=3)\n",
    "my_knn.fit(x_train,y_train)\n",
    "my_knn.predict(x_test)\n",
    "my_knn.score(x_test,y_test)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "寻找最好的K"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最优K： 3\n",
      "最优匹配度： 0.9673157162726008\n"
     ]
    }
   ],
   "source": [
    "best_score=0.0\n",
    "best_k=-1\n",
    "for k in range(1,11):\n",
    "    knn_clf=KNeighborsClassifier(n_neighbors=k)\n",
    "    knn_clf.fit(x_train,y_train)\n",
    "    score=knn_clf.score(x_test,y_test)\n",
    "    if score>best_score:\n",
    "        best_score=score\n",
    "        best_k=k\n",
    "print(\"最优K：\",best_k)\n",
    "print(\"最优匹配度：\",best_score)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最优K： 4\n",
      "最优匹配度： 0.9728789986091794\n",
      "最优方法: distance\n"
     ]
    }
   ],
   "source": [
    "best_METHOD=\"\"\n",
    "best_score=0.0\n",
    "best_k=-1\n",
    "for method in [\"uniform\",\"distance\"]:#uniform不考虑距离权重，distance考虑距离权重\n",
    "    for k in range(1,11):\n",
    "        knn_clf=KNeighborsClassifier(n_neighbors=k,weights=method)\n",
    "        knn_clf.fit(x_train,y_train)\n",
    "        score=knn_clf.score(x_test,y_test)\n",
    "        if score>best_score:\n",
    "            best_score=score\n",
    "            best_k=k\n",
    "            best_METHOD=method\n",
    "print(\"最优K：\",best_k)\n",
    "print(\"最优匹配度：\",best_score)\n",
    "print(\"最优方法:\",best_METHOD)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "明可夫斯基模型-超参数p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最优K： 4\n",
      "最优匹配度： 0.9770514603616134\n",
      "最优方法: distance\n",
      "最优P: 4\n"
     ]
    }
   ],
   "source": [
    "%%\n",
    "best_METHOD=\"\"\n",
    "best_score=0.0\n",
    "best_k=-1\n",
    "best_p=-1\n",
    "for method in [\"uniform\",\"distance\"]:#uniform不考虑距离权重，distance考虑距离权重\n",
    "    for p in range(1,6):#明可夫斯基p\n",
    "        for k in range(1,11):\n",
    "            knn_clf=KNeighborsClassifier(n_neighbors=k,weights=method,p=p)\n",
    "            knn_clf.fit(x_train,y_train)\n",
    "            score=knn_clf.score(x_test,y_test)\n",
    "            if score>best_score:\n",
    "                best_score=score\n",
    "                best_k=k\n",
    "                best_METHOD=method\n",
    "                best_p=p\n",
    "print(\"最优K：\",best_k)\n",
    "print(\"最优匹配度：\",best_score)\n",
    "print(\"最优方法:\",best_METHOD)\n",
    "print(\"最优P:\",best_p)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
